//JS
layui.use(['element', 'layer', 'util', 'laydate'], function () {
    let element = layui.element,
        util = layui.util,
        laydate = layui.laydate,
        table = layui.table,
        $ = layui.$;

    <!-- 动态表格 -->
    table.render({
        elem: '#replyReviewerAssign-table',
        toolbar: true,
        page: true,
        height: 700,
        url: "/getAllTeacher",
        cols: [
            [ //标题栏
                {field: 'tNO', title: '工号', sort: true, width: 120, align: 'center'},
                {field: 'name', title: '真实姓名', width: 120, align: 'center'},
                {field: 'sex', title: '性别', width: 90, align: 'center'},
                {field: 'collegeId', title: '学院编号', align: 'center'},
                {field: 'department', title: '部门', width: 110, align: 'center'},
                {field: 'post', title: '职位', width: 110, align: 'center'},
                {field: 'tel', title: '联系方式', width: 90, align: 'center'},
                {field: 'email', title: '邮箱', align: 'center'},
                {title: '操作', width: 120, toolbar: '#replygroupAssignBtn', align: 'center'}
            ]
        ]
    });
    //监听行工具事件
    table.on('tool(replyReviewerAssign-table)', function (obj) {
        let data = obj.data;
        if (obj.event === 'replygroupA') {//指定为答辩组教师
            layer.confirm('确认分配该教师为答辩组教师吗？', function () {
                let tno = data.tNO;
                // console.log(tno)

                let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
                $.ajax({
                    url: "/replyReviewerAssign"
                    , type: 'post'
                    , data: {
                        tno: tno
                    }
                    , dataType: 'json'
                    , success: function (data) {//调用接口成功
                        console.log(data)
                        table.reload('replygroupAssign-table');
                        if (data === 1001) {//评阅人不存在，进行新增操作
                            layer.close(loadPage);
                            layer.closeAll('page');
                            layui.layer.msg('分配成功！', {offset: '300px', time: 2500, anim: 0});
                            table.reload('replyReviewerAssign-table', {});
                        } else if (data === 1002) {
                            layui.layer.close(loadPage);
                            layui.layer.alert('分配失败！');
                        } else if (data === 1003) {
                            layui.layer.close(loadPage);
                            layui.layer.alert('分配失败！该教师已被分配过！');
                        }
                    }
                });
            });
            return false;//阻止表单跳转。如果需要表单跳转，去掉这段即可。
        }
    });


    table.render({
        elem: '#replygroupAssign-table',
        toolbar: true,
        page: true,
        height: 700,
        where: {roleName: '答辩组教师'},
        url: "/getTeacherByRole",
        cols: [
            [ //标题栏
                {field: 'tNO', title: '工号', sort: true, width: 120, align: 'center'},
                {field: 'name', title: '真实姓名', width: 120, align: 'center'},
                {field: 'sex', title: '性别', width: 90, align: 'center'},
                {field: 'collegeId', title: '学院名称', align: 'center'},
                {field: 'department', title: '部门', width: 110, align: 'center'},
                {field: 'post', title: '职位', width: 110, align: 'center'},
                {field: 'tel', title: '联系方式', width: 150, align: 'center'},
                {field: 'email', title: '邮箱', align: 'center'},
                /* {title: '操作', width: 120, toolbar: '#replygroupAssignBtn', align: 'center'}*/
            ]
        ]
    });


    //分配答辩小组
    $("#allotGroup").click(function () {
        //判断是否分配
        let data = table.getData('group-table');
        if (data.length > 0) {
            layer.msg('你已分配答辩小组，如需要选择重新分配')
            return false;
        }
        let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
        $.ajax({
            url: '/allotGroup',
            type: 'get',
            data: {
                type: 1
            },
            dataType: 'json',
            success: function (data) {  //调用接口成功
                allotRes(data, loadPage);
            },
            error: function () {  //接口错误
                layui.layer.close(loadPage);
                layui.layer.alert('服务器出错啦  (｡•́︿•̀｡)');
            }
        });

    });

    /*分配学生*/
    $("#allotStu").click(function () {
        let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
        $.ajax({
            url: '/allotStu',
            type: 'get',
            dataType: 'json',
            success: function (data) {  //调用接口成功
                layer.close(loadPage);
                if (data === 1001 || data === '1001') {
                    layer.msg('随机分配成功');
                    table.reload('paper-table');
                    table.reload('group-table');
                } else if (data === 1001 || data === '1001') {
                    layer.msg('还未分配答辩教师小组');
                } else {
                    layui.layer.alert('分配失败');
                }
            },
            error: function () {  //接口错误
                layui.layer.close(loadPage);
                layui.layer.alert('服务器出错啦  (｡•́︿•̀｡)');
            }
        });
    });

    //重新分配答辩小组
    $("#resetGroup").click(function () {
        let loadPage = layui.layer.load(0, {shade: [0.5, '#fff']});
        $.ajax({
            url: '/allotGroup',
            type: 'get',
            data: {
                type: 2
            },
            dataType: 'json',
            success: function (data) {  //调用接口成功
                allotRes(data, loadPage);
            },
            error: function () {  //接口错误
                layui.layer.close(loadPage);
                layui.layer.alert('服务器出错啦  (｡•́︿•̀｡)');
            }
        });
    });


    function allotRes(data, loadPage) {
        //1001 成功  1002 失败 1003 答辩教师人数不足4人 无法分配
        table.reload('group-table');
        layui.layer.close(loadPage);
        if (data === 1001 || data === '1001') {
            layui.layer.msg('分配成功', {offset: '300px', time: 2500, anim: 0});
        } else if (data === 1002 || data === '1002') {
            layui.layer.msg('分配失败', {offset: '300px', time: 2500, anim: 0});
        } else if (data === 1003 || data === '1004') {
            layui.layer.msg('答辩教师人数不足4人 无法分配', {offset: '300px', time: 2500, anim: 0});
        } else {
            layui.layer.msg('分配失败', {offset: '300px', time: 2500, anim: 0});
        }
    }


    /*答辩小组的信息*/
    table.render({
        elem: '#group-table',
        toolbar: true,
        page: true,
        height: 700,
        where: {roleName: '答辩组教师'},
        url: "/getAllGroup",
        cols: [
            [ //标题栏
                {field: 'id', title: 'Id', sort: true, width: 120, align: 'center'},
                {field: 'groupNO', title: '编号', width: 80, align: 'center'},
                {field: 'groupLeader', title: '组长工号', width: 130, align: 'center'},
                {field: 'groupMember', title: '答辩小组成员', align: 'center'},
                {field: 'groupTime', title: '答辩时间', width: 180, align: 'center'},
                {field: 'groupAddress', title: '答辩地点', width: 150, align: 'center'},
                {field: 'stuSum', title: '已有学生人数', width: 150, align: 'center'},
                {title: '操作', width: 120, toolbar: '#addressAssignBtn', align: 'center'}
            ]
        ]
    });


    laydate.render({
        elem: '#replyTime'
        , type: 'datetime'
    });
    /*监听操作栏*/
    let GroupId;
    table.on('tool(group-table)', function (obj) {
        let data = obj.data;  //json格式的数据
        if (obj.event === 'addTimeAndAddress') {
            GroupId = data.id;
            console.log(GroupId);
            layer.open({
                type: 1
                , offset: 50
                , skin: 'layui-layer-rim'
                , area: ['740px', '500px']
                , content: $("#addTimeAndAddress")
                , title: '分配答辩时间和地点'
                , success: function (layerObj) {
                    $(".layui-layer-shade").appendTo(layerObj.parent());
                    $("#replyId").html(data.groupNO);
                    $("#replyLeader").html(data.groupLeader);
                    $("#replyMember").html(data.groupMember);
                }
                , end: function () {
                    $("#addTimeAndAddress").css("display", 'none');
                }
            });
        }

    });

    /*分配时间和地点*/
    $("#addTimeBtn").click(function () {
        let time = $("#replyTime").val().trim();
        let address = $("#replyAddress").val().trim();
        if (time === null || time === '' || address === '' || address === null) {
            layer.msg('时间和地点不能为空');
            return false;
        }
        let url = '/addTimeAndAddress';
        let param = {replyTime: time, replyAddress: address, id: GroupId};
        $.post(url, param, function (data) {
            if (data === true || data === 'true') {
                table.reload('group-table');
                layer.msg('分配成功');
                layer.closeAll('page');
            } else {
                layer.msg('分配成功');
            }
        });


    });

    <!-- 论文信息表 -->
    table.render({
        elem: '#paper-table'
        , toolbar: '#task-table-toolbar'
        , height: 700
        , url: "/getPaper"
        , where: {queryKey: '', state: '定稿', type: 3}
        , cols: [
            [ //标题栏
                {field: 'bh', title: '编号', width: 90, align: 'center', sort: true},
                {field: 'title', title: '标题', align: 'center'},
                {field: 'sno', title: '学号', width: 140, align: 'center'},
                {field: 'sname', title: '姓名', width: 110, align: 'center'},
                {
                    field: 'title1', title: '所属学院', width: 130, align: 'center', templet: function (res) {
                        return res.title1.collegeName;
                    }
                },
                {
                    field: 'title1', title: '所属专业', width: 150, align: 'center', templet: function (res) {
                        return res.title1.majorName;
                    }
                },
                {
                    field: 'title1', title: '指导老师', width: 110, align: 'center', templet: function (res) {
                        return res.title1.tName;
                    }
                },
                {
                    field: 'title1', title: '老师工号', width: 130, align: 'center', templet: function (res) {
                        return res.title1.tno;
                    }
                },
                {
                    field: 'task', title: '检查结果', width: 130, align: 'center', templet: function (res) {
                        if (res.stageCheck === null) {
                            return '<span style="color: #999999">无结果</span>'
                        } else if (res.stageCheck.result === null || res.stageCheck.result === '') {
                            return '<span style="color: #999999">无结果</span>'
                        } else if (parseFloat(res.stageCheck.result) < 0.30) {
                            return '<span style="color: #009f95">' + res.stageCheck.result + '</span>';
                        } else if (parseFloat(res.stageCheck.result) > 0.30) {
                            return '<span style="color: red">' + res.stageCheck.result + '</span>';
                        } else {
                            return '<span style="color: #999999">无结果</span>'
                        }
                    }
                },
                {
                    field: 'state', title: '论文阶段', width: 130, align: 'center', templet: function (res) {
                        if (res.state === '定稿') {
                            return '<span style="color: #009688">定稿</span>'
                        } else {
                            return '<span style="color: #999999">' + res.state + '</span>'
                        }

                    }
                },
                {
                    field: 'task', title: '定稿', width: 140, align: 'center', templet: function (res) {
                        if (res.stageCheck === null) {
                            return '<span style="color: red">待提交</span>'
                        } else {
                            if (res.stageCheck.state === null || res.stageCheck.state === '') {
                                return '<span style="color: red">待提交</span>'
                            } else if (res.stageCheck.state === '审核中') {
                                return '<span style="color: blue">审核中</span>'
                            } else if (res.stageCheck.state === '审核完成') {
                                return '<span style="color: #009688">审核完成</span>'
                            } else {
                                return '<span style="color: red">待提交</span>'
                            }
                        }
                    }
                },
                {field: 'replyGroup', title: '答辩组编号', width: 120, align: 'center'},
                {field: 'title1', title: '课题来源', width: 120, align: 'center', hide: true},
                {field: 'stageCheck', title: '课题类别', width: 100, align: 'center', hide: true},
            ]
        ]
        // ,skin: 'line' //表格风格
        , even: true
        , page: true
        , limits: [11, 22, 33]
        , limit: 11
        , text: {
            none: '<div class="layui-anim layui-anim-scaleSpring"  style="margin-top: 150px"><i class="layui-icon" style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><br><br><br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有没找到数据呢</div></div>'
            ,
            error: '<div style="margin-top: 150px"><i class="layui-icon" style="color: #009f95;font-size: 150px;border-bottom: #009f95 solid 2px;padding:15px;margin-top: 50px" >&#xe664;</i><br>' +
                '<br><div class="layui-text" style="font-size: 25px;margin-top: 25px;color: #009f95">好像没有出错了呢</div></div>'
        }
    });


    <!-- 弹出层-批量增加动态表格 -->
    table.render({
        elem: '#replygroupAssign-sshow-table',
        toolbar: '#replygroupAssignToolBar',
        page: true,
        height: 500,
        cols: [
            [ //标题栏
                {type: 'checkbox'},
                {field: 'title', title: '课题名称'},
                {field: 'major', title: '专业'},
                {field: 'sno', title: '选题学生学号'},
                {field: 'sname', title: '学生姓名'},
                {field: 'tno', title: '指导教师工号'},
                {field: 'tname', title: '教师姓名'},
                {title: '操作', toolbar: '#replygroupAssign-sshowBtn'}
            ]
        ],
        data: [
            {"title": "《母猪的产后护理》", "major": "测试专业", "sno": "stu-01", "sname": "学生01", "tno": "tea-01", "tname": "教师01"}
        ]
    });
    <!-- 弹出层-批量增加动态表格 -->
    table.render({
        elem: '#replygroupAssign-assignQuery-table',
        page: true,
        height: 500,
        cols: [
            [ //标题栏
                {type: 'checkbox'},
                {field: 'title', title: '课题名称'},
                {field: 'sno', title: '选题学生学号', width: 180},
                {field: 'sname', title: '学生姓名', width: 150},
                {field: 'tno', title: '指导教师工号', width: 180},
                {field: 'tname', title: '教师姓名', width: 150},
            ]
        ],
        data: [
            {"title": "《母猪的产后护理》", "sno": "stu-01", "sname": "学生01", "tno": "tea-01", "tname": "教师01"}
        ]
    });


});